//
//  MeMemuView.m
//  FKGW
//
//  Created by song ce on 2017/2/17.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "MeMemuView.h"
#import "UIView+Extend.h"
#import "BaseNavigationViewController.h"
#import "MeCollectionCell.h"
#import "LoginViewController.h"
#import "BaseViewController.h"

#import "BaseMeViewController.h"
#define KColumCount 3

@implementation MeMemuView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
//         _messageCout = @[@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0"].mutableCopy;
        [self _initSubviews];
        [self loadData];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(resetContLabel:) name:@"resetContLabel" object:nil];
    }
    return self;
}
-(void)setMessageCout:(NSMutableArray *)messageCout{
    _messageCout = messageCout;
    if(self.dataSoure.count){
        [self.dataSoure removeAllObjects];
    }
    [self loadData];
    [_myCollectionView reloadData];
}

////ceshi
//-(void)resetContLabel:(NSNotification*)noti{
//    NSString *rowIndex  = noti.object;
//    if(rowIndex.length){
//        int count = [rowIndex intValue];
//        [_messageCout replaceObjectAtIndex:count withObject:@"0"];
//        if(self.dataSoure.count){
//            [self.dataSoure removeAllObjects];
//        }
//        [self loadData];
//        [_myCollectionView reloadData];
//    }
//}

- (NSMutableArray*)dataSoure{
    if(_dataSoure==nil){
        _dataSoure = [NSMutableArray array];
    }
    return _dataSoure;
}

- (void)loadData{
    
       NSArray *imageArr = @[@"icon_wodeguanzhu_nor",
                          @"icon_wodeshoucang_nor",
                          @"icon_daishouhuo_nor",
                          @"icon_gouwuche_nor",
                          @"icon_daifukuan_nor",
                          @"icon_daipingjia_nor",
                          @"icon_qianbao_nor",
                          @"icon_daifahuo_nor",
                          @"icon_yiwancheng",

                          ];
    NSArray *nameArr = @[@"我的关注",
                          @"我的收藏",
                         @"待收货",
                         @"我的购物车",
                         @"待付款",
                         @"待评价",
                         @"钱包",
                          @"待发货",
                         @"已完成订单"];
    
     NSArray *classArr =
                      @[@"AttentionViewController",
                           @"ColelctViewController",
                           @"WaitReceiveViewController",
                          @"ShoppingCartViewController",
                          @"WaitPayViewController",
                          @"WaitEvaluateViewController",
                          @"MoneyViewController",
                          @"WaitToSendViewController",
                          @"SaleReturnViewController"];
    
    for(int i = 0; i<imageArr.count;i++){
        [self addItemWithTitle:nameArr[i] imageName:imageArr[i] class:NSClassFromString(classArr[i]) messageCount:_messageCout[i]];
    }
}


- (void)addItemWithTitle:(NSString *)title imageName:(NSString *)imageName class:(Class)class messageCount:(NSString*)messageCount
{
    MeCollectionModel *item = [[MeCollectionModel alloc]init];
    item.title = title;
    item.iamgeName =imageName;
    item.class = class;
    item.messgeCount = messageCount;
    [self.dataSoure addObject:item];
}

- (void)_initSubviews{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个item的大小
    layout.itemSize = CGSizeMake(50,50);
    //每一行之间的最小距离
    layout.minimumLineSpacing = 0;
    //同一行item之间的最小间隙
    layout.minimumInteritemSpacing =0;
    //设置滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    //设置分页
    collectionV.pagingEnabled = YES;
    [self addSubview:collectionV];
    collectionV.backgroundColor = [UIColor whiteColor];
    //设置代理
    collectionV.delegate = self;
    collectionV.dataSource = self;
    //注册
    [collectionV registerNib:[UINib nibWithNibName:@"MeCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"MeCollectionCell"];
    _myCollectionView = collectionV;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    return self.dataSoure.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    MeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MeCollectionCell" forIndexPath:indexPath];
    MeCollectionModel *item = self.dataSoure[indexPath.row];
    cell.model = item;
    
    if(indexPath.row%2==0){
        cell.backgroundColor = [UIColor colorWithRed:250./255 green:250./255 blue:250./255 alpha:1];
    }else{
        cell.backgroundColor = [UIColor colorWithRed:255./255 green:255./255 blue:255./255 alpha:1];
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(Main_Screen_Width/KColumCount, self.height/KColumCount);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(![AppDelegate appDealegate].uid.length){
        LoginViewController *login = [[LoginViewController alloc]init];
        login.isModel = YES;
        BaseNavigationViewController *baseNavi = [[BaseNavigationViewController alloc]initWithRootViewController:login];

        [[self baseViewController] presentViewController:baseNavi animated:YES completion:nil];
        return;
    }
    
    if(indexPath.row==6){
        [MBProgressHUD showTipMessageInWindow:@"该功能正在开发\r\n敬请期待..."];

        return;
    }
    MeCollectionModel *item = self.dataSoure[indexPath.row];
    if (item.class) {
        BaseMeViewController *vc = [[item.class alloc]init];
        vc.vcName = item.title;
//        vc.rowIndex = [NSString stringWithFormat:@"%ld",indexPath.row];
        vc.hidesBottomBarWhenPushed = YES;
        [[self secondNavigationController] pushViewController:vc animated:YES];
    }
}

- (BaseNavigationViewController*)secondNavigationController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseNavigationViewController class]]){
            return (BaseNavigationViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}
- (BaseMeViewController*)baseViewController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseMeViewController class]]){
            return (BaseMeViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}


@end
